import os
import requests

def fetch(url,imageDir):
    try:
        if not os.path.exists(imageDir):
            os.mkdir(imageDir)

        imagePath = imageDir + '/' + url.split('/')[-1]

        if not os.path.exists(imagePath):
            r = requests.get(url)
            r.raise_for_status()
            with open(imagePath,'wb') as f:
                f.write(r.content)
                f.close()
                print('save success')

        else:
            print('file exists')
    except:
        print('fetch fail')


if __name__ == '__main__':
    url = 'http://img.1985t.com/uploads/attaches/2016/01/60320-bbZEcch.jpg'
    imageDir = 'F://images'
    fetch(url,imageDir)
